<template>
  <div class="broker">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>经纪人管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="24">
        <el-col :span="8">
          <span>搜索：</span
          ><el-input
            v-model="queryInfo.keyword"
            @input="keyWordChang"
            placeholder="请输入经纪人姓名/手机号"
          ></el-input>
        </el-col>
        <el-col :span="8">
          <span>状态：</span>
          <el-select v-model="value"
            @change="changeType"
            placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button type="primary">查询</el-button>
          <el-button type="primary" @click="addInform">新建</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table :data="result.length?result:tableData" style="width: 100%">
      <el-table-column label="序号" prop="id" width="80px"></el-table-column>
      <el-table-column label="姓名" prop="name" width="80px"></el-table-column>
      <el-table-column
        label="手机号"
        prop="tel"
        width="120px"
      ></el-table-column>
      <el-table-column
        label="公司"
        prop="company"
        width="120px"
      ></el-table-column>
      <el-table-column label="新房" prop="num" width="80px"></el-table-column>
      <el-table-column
        label="二手房"
        prop="nums"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="租房"
        prop="renting"
        width="80px"
      ></el-table-column>
      <el-table-column label="状态" prop="type" width="80px"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)" type="primary"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button size="mini">启动</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="经纪人管理" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.tel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="公司" :label-width="formLabelWidth">
          <el-input v-model="form.company" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新房" :label-width="formLabelWidth">
          <el-input v-model="form.num" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="二手房" :label-width="formLabelWidth">
          <el-input v-model="form.nums" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="租房" :label-width="formLabelWidth">
          <el-input v-model="form.renting" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-input v-model="form.type" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="Submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      options: [
        {
          value: '选项1',
          label: '请选择'
        },
        {
          value: '选项2',
          label: '在职'
        },
        {
          value: '选项3',
          label: '离职'
        }
      ],
      value: '',
      keyword: '',
      queryInfo: {
        keyword: ''
      },
      dialogFormVisible: false,
      form: {
        tel: '',
        name: '',
        company: '',
        num: '',
        nums: '',
        renting: '',
        type: ''
      },
      formLabelWidth: '120px',
      tableData: [],
      result: []
    }
  },
  methods: {
    changeType (val) {
      this.result = this.tableData.filter(item => {
        return item.type.includes(val)
      })
    },
    addInform () {
      this.form = {
        tel: '',
        name: '',
        company: '',
        num: '',
        nums: '',
        renting: '',
        type: ''
      }
      this.dialogFormVisible = true
    },
    Submit () {
      if (this.form.id) {
        // 编辑
        this.$http.put('/broker/edit', this.form).then((res) => {
          if (res.data.code === 1) {
            this.$message.success(res.data.message)
            this.getBrokerList(this.queryInfo)
          } else {
            this.$message.error(res.data.message)
          }
        })
      } else {
        // 添加
        this.$http.post('/broker/add', this.form).then((res) => {
          if (res.data.code === 1) {
            this.$message.success(res.data.message)
            this.getBrokerList(this.queryInfo)
          } else {
            this.$message.error(res.data.message)
          }
        })
      }
      this.dialogFormVisible = false
    },
    handleDelete (row) {
      this.$http.delete(`/broker/${row.id}`).then((res) => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.message)
        } else {
          this.$message.success(res.data.message)
          this.getBrokerList(this.queryInfo)
        }
      })
    },
    keyWordChang () {
      this.getBrokerList(this.queryInfo)
    },
    handleEdit (row) {
      this.form = row
      this.dialogFormVisible = true
    },
    getBrokerList () {
      this.$http
        .get('/broker', {
          params: this.queryInfo
        })
        .then((res) => {
          if (res.data.code === 1) {
            this.tableData = res.data.data
          }
        })
    }
  },
  created () {
    this.getBrokerList(this.queryInfo)
  }
}
</script>

<style scoped>
.el-row,
.el-col {
  display: flex;
  align-items: center;
}

.el-col span {
  width: 72px;
}
</style>
